

/*  @auth ：   nxs@winning.com.cn
 *   @remark:  json- 填充form表单
 *   @Date:    2018-07-12 09:18:54
 *   json格式如下：  var jsonstring = "uname:'Alicezhang',nikename:'美女',sex:'0',married:'no'," +
 *            "fav:'足球|羽毛球',standard:'帅|富',city:'other'," +
 *            "headpic:'./images/head2.jpg',pwd:'1234567890',eduhistory:'i am a student!'"
 *        alert("要填充的json及填充前的界面:" + jsonstring);
 *  调用详见 json-form-demo.html
 */

String.prototype.lTrim = function ()　　 {
    return this.replace(/^'/, "");
}; //去掉左面空格;

String.prototype.rTrim = function ()　　 {
    return this.replace(/'*$/, "");
}; //去掉右面空格;

String.prototype.Trim = function ()　　 {
    return this.lTrim().rTrim();
}; //记得各句后都有分号

//
$.fn.jsontoform = function (jsonstring) {
    var formid = "#" + $(this).attr("id"); //input,textarea,select;
    var objs = jsonstring.split(',');
    for (i = 0; i < objs.length; i++) {
        var kvs = objs[i].split(':');
        //alert(kvs.length);
        var k = kvs[0];
        var v = kvs[1].Trim();
        //alert(k+"  "+v);
        selector = "[name='" + k + "']";
        //alert(selector);
        //alert($(selector).length);
        if ($(selector).length > 0) {
            for (j = 0; j < $(selector).length; j++) {
                //text or password
                if ($(selector).attr("type") == "text" || $(selector).attr("type") == "password") {
                    $(selector).val(v);
                }
                //combo select
                if ($(selector).attr("type") == null && $(selector).length == 1) {
                    if ($("select" + selector).length == 1) {
                        for (n = 0; n < $(selector + " option").length; n++) {
                            //alert(n+":"+$(selector+" option:eq("+n+")").val()+"["+$(selector+" option:eq("+n+")").html()+"]==?=="+v);
                            if ($(selector + " option:eq(" + n + ")").val() == v) {
                                $(selector + " option:eq(" + n + ")").attr("selected", true);
                                break;
                            }
                        };
                    }
                }
                // checkbox
                if ($(selector).attr("type") == "checkbox") {
                    var checkboxselector = "input[type='checkbox'][name='" + k + "']";
                    var options = v.split('|');
                    for (m = 0; m < options.length; m++) {
                        for (k = 0; k < $(checkboxselector).length; k++) {
                            if ($(checkboxselector + ":eq(" + k + ")").val() == options[m]) {
                                $(checkboxselector + ":eq(" + k + ")").attr("checked", true);
                            }
                        }
                    }
                }
                //readio
                if ($(selector).attr("type") == "radio") {
                    var radioselector = "input[type='radio'][name='" + k + "']";
                    for (k = 0; k < $(radioselector).length; k++) {
                        if ($(radioselector + ":eq(" + k + ")").val() == v) {
                            $(radioselector + ":eq(" + k + ")").attr("checked", true);
                        }
                    }
                }

                //textarea
                if ($("textarea[name='" + k + "']").length == 1) {
                    $("textarea[name='" + k + "']").val(v);
                }


            }
        }

    }
};




//  转换二
$.fn.populateForm = function(data){
    return this.each(function(){
        var formElem, name;
        if(data == null){this.reset(); return; }
        for(var i = 0; i < this.length; i++){
            formElem = this.elements[i];
            //checkbox的name可能是name[]数组形式
            name = (formElem.type == "checkbox")? formElem.name.replace(/(.+)\[\]$/, "$1") : formElem.name;
            if(data[name] == undefined) continue;
            switch(formElem.type){
                case "checkbox":
                    if(data[name] == ""){
                        formElem.checked = false;
                    }else{
                        //数组查找元素
                        if(data[name].indexOf(formElem.value) > -1){
                            formElem.checked = true;
                        }else{
                            formElem.checked = false;
                        }
                    }
                    break;
                case "radio":
                   /* if(data[name] == ""){
                        formElem.checked = false;
                    }else if(formElem.value == data[name]){
                        formElem.checked = true;
                    }*/
                   if(formElem.value == data[name]){
                        formElem.checked = true;
                    }else{
                       formElem.checked = false;
                   }
                    break;
                case "button": break;
                default: formElem.value = data[name];
            }
        }
    });
};









